package com.gong.suanfa.DFS;

import java.util.Stack;

/**
 * @author GongChang
 * @version 1.0
 * @date 2021/6/14 0014 9:15
 */
class Node{

    public Node(int val){
        this.val = val;
    }

    int val;
    Node left;
    Node right;
}

public class Dfs {
    public static void main(String[] args) {
        Node node = new Node(1);
        node.left = new Node(2);
        node.left.left = new Node(4);
        node.left.right = new Node(5);
        node.right = new Node(3);

        Stack stack = new Stack();

        dfs(node,stack);
    }

    public static void dfs(Node node, Stack stack){
        //入栈
        stack.push(node);
        if (node.left != null){
            dfs(node.left,stack);
        }
        if (node.right != null){
            dfs(node.right,stack);
        }

        Node pop = (Node) stack.pop();
        System.out.println(pop.val);
    }

}
